const express = require("express");
const fs = require("fs");

const app = express();

//创建路由规则
app.get("/software", (req, res) => {
  //读取 software 文件夹中的文件列表
  let files = fs.readdirSync("./software");
  //转为字符串响应
  // res.send(files.toString());
  //遍历响应
  res.set("content-type", "text/html;charset=utf-8");
  res.write(`<!DOCTYPE html>
              <html lang="en">
              <head>
                <meta charset="UTF-8">
                <meta http-equiv="X-UA-Compatible" content="IE=edge">
                <meta name="viewport" content="width=device-width, initial-scale=1.0">
                <title>Document</title>
              </head>
              <body>`);
  files.forEach((item) => {
    res.write(`<a href='/download/${item}'>` + item + "</a><br>");
  });

  res.write(`</body>
  </html>`)
  res.end();
});

app.get("/download/:id", (req, res) => {
  //响应下载
  setTimeout(() => {
    // res.send('文件下载');
    res.download(`./software/${req.params.id}`);
  }, 2000);
});

app.listen(80, () => {
  console.log("启动成功...");
});
